package com.xiang._01;

import com.google.common.base.Joiner;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * 字符串拼接
 */
public class JoinTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(0, 1, 3);

        //1.StringJoiner
        String s = join3(list);
        System.out.println(s);


        //2. java8 stream
        String collect = list.stream().map(String::valueOf).collect(Collectors.joining(","));
        System.out.println(collect);

        //3. guava joiner
        String join = Joiner.on(",").join(list);
        System.out.println(join);


    }

    public static String join3(List<Integer> list) {
        StringJoiner joiner = new StringJoiner(",");
        for (Integer s : list) {
            joiner.add(String.valueOf(s));
        }
        return joiner.toString();
    }
}
